#include "QBrick.h"


Qbrick::Qbrick(float posX, float posY, float width, float height, Resources* res, int kind, int number, int depth)
:GameObject(posX, posY, width, height, res, depth, number)
{
	_sprite = new Sprite(_resources->QBrick, 7, 7, 0.2f);
	_id = ID::ID_QBrick;

	_vel_x = _vel_y = 0;
	_acc_x = _acc_y = 0;
	_old_y = _pos_y;

	_state = QBRICK_STATE::QBRICK_NORMAL;

	_isStatic = true;
	
	// In QBrick, property _moreover save kind object in this
	switch (kind)
	{
	case ID::ID_Coin:
		_moreover = ID::ID_Coin;
		break;
	case ID::ID_MushGreen:
		_moreover = ID::ID_MushGreen;
		break;
	case ID::ID_MushRed:
		_moreover = ID::ID_MushRed;
		break;
	case ID::ID_Flower:
		_moreover = ID::ID_Flower;
		break;
	case ID::ID_Star:
		_moreover = ID::ID_Star;
		break;
	default:
		break;
	}
	
}


Qbrick::~Qbrick()
{
}

void Qbrick::Update(float time, QNode* &Tree)
{
	//
	// update
	switch (_state)
	{
	case QBRICK_STATE::QBRICK_NORMAL:
		_vel_y = _acc_y = 0;
		break;
	case QBRICK_STATE::QBRICK_MOVEUP:
		_vel_y += _acc_y * time;
		_pos_y -= _vel_y * time + 0.5f * _acc_y * time * time;

		if (_pos_y < _old_y)
		{
			ChangeState(QBRICK_STATE::QBRICK_STONE);
		}

		break;
	case QBRICK_STATE::QBRICK_STONE:
		_vel_y = _acc_y = 0;
		break;
	default:
		break;
	}

	// Update animation
	if (_state == QBRICK_STATE::QBRICK_NORMAL)
		_sprite->Next(0, 5, time);
}

void Qbrick::Render(LPD3DXSPRITE spriteHandler, Viewport* viewport)
{
	_sprite->Render(spriteHandler, _pos_x, _pos_y, false, viewport);
}

void Qbrick::ChangeState(char state)
{
	if (_state == QBRICK_STATE::QBRICK_STONE)
	{
		_sprite->Next(6);
		return;
	}

	_state = state;

	switch (_state)
	{
	case QBRICK_STATE::QBRICK_NORMAL:
		_vel_y = 0;
		_acc_y = 0;
		_pos_y = _old_y;
		ResetRect();
		break;
	case QBRICK_STATE::QBRICK_MOVEUP:
		_vel_y = -VEL_Y;
		_acc_y = GRAVITY;
		break;
	default:
		_pos_y = _old_y;
		_sprite->Next(6);
		break;
	}
}